package com.laiketui.task.services.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laiketui.common.api.PublicUserService;
import com.laiketui.common.mapper.OrderReportModelMapper;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.report.OrderReportModel;
import com.laiketui.task.services.interfaces.TaskService;
import com.laiketui.task.services.interfaces.UserTaskService;
import com.xxl.job.core.context.XxlJobHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户定时任务实现类
 *
 * @author ganpeng
 * @date 2023/05/12 17:50
 */
@Service
public class UserTaskServiceImpl implements UserTaskService {
    @Autowired
    private PublicUserService publicUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderReportModelMapper orderReportModelMapper;

    @Autowired
    private TaskService taskService;
    /***
     * 定时缓存平台新增用户信息
     * @throws LaiKeAPIException
     * @author ganpeng
     * @date 2023/05/12 17:50
     */
    @Override
    public void AdditionUserData() throws LaiKeAPIException {
        List<Integer> storeIdList = null;
        try {
            XxlJobHelper.log("缓存平台新增用户信息 开始执行!");
            //获取所有商城id缓存
            if (redisUtil.get(GloabConst.RedisHeaderKey.LKT_STOREID_EFFECTIVE_LIST) != null){
                storeIdList = JSON.parseObject(redisUtil.get(GloabConst.RedisHeaderKey.LKT_STOREID_EFFECTIVE_LIST).toString(), new TypeReference<List<Integer>>() {});
            }
            if (storeIdList == null || storeIdList.size() == 0){
                XxlJobHelper.log("商城id缓存为空");
                storeIdList = taskService.getStoreIdAll();
            }
            storeIdList.stream().forEach(storeId -> {
                OrderReportModel model = new OrderReportModel();
                orderReportModelMapper.deleteByType(storeId, 5);
                model.setType(5);
                model.setNum(0);
                model.setStoreId(storeId);
                model.setData(JSON.toJSONString(publicUserService.getAdditionUserData(storeId)));
                int insert = orderReportModelMapper.insert(model);
                if (insert < 1){
                    XxlJobHelper.log("缓存平台新增用户信息失败 商城id:{}", storeId);
                }else {
                    XxlJobHelper.log("缓存平台新增用户信息完成 商城id:{}", storeId);
                }
                model = null;
            });

            XxlJobHelper.log("缓存平台新增用户信息 完成!");
        }catch (Exception e){
            e.printStackTrace();
            XxlJobHelper.log("缓存平台新增用户信息 异常: " + e.getMessage());
        } finally {
            storeIdList = null;
            XxlJobHelper.handleSuccess("缓存平台新增用户信息 开始执行!");
        }

    }
}
